package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.MongoDbConstant;
import com.bbzn.device.client.dataobject.*;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wulongbo
 * @Date : 2021/4/9 11:19
 * @Version 1.0
 */
@Component
public class EmqWearStrategy implements EmqStrategy {

    @Autowired
    private SimpleMongoService simpleMongoService;

    @Autowired
    private DeviceService deviceService;

    // 佩戴上报体温，环境温度，海拔
    //{"imei":"867400020316612","code":"4032","temperate":"36.2","temperature":"27.45","elevation":"0","humidity":65,"aqi":80}
    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSON.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (!OptionalBean.ofNullable(imei).isPresent()) {
                return "json解析错误";
            }
            //体温
            Double temperate = jsonObject.getDouble("temperate");
            if (!OptionalBean.ofNullable(temperate).isPresent()) {
                return "json解析错误";
            }
            //环境温度
            Double temperature = jsonObject.getDouble("temperature");
            if (!OptionalBean.ofNullable(temperature).isPresent()) {
                return "json解析错误";
            }
            //海拔
            String elevation = jsonObject.getString("elevation");
            if (!OptionalBean.ofNullable(elevation).isPresent()) {
                return "json解析错误";
            }
            //湿度
            String humidity = jsonObject.getString("humidity");
            if (!OptionalBean.ofNullable(humidity).isPresent()) {
                return "json解析错误";
            }
            //空气质量
            String aqi = jsonObject.getString("aqi");
            if (!OptionalBean.ofNullable(aqi).isPresent()) {
                return "json解析错误";
            }

            /**
             * 数据类型 0 心率 1 计步 2 电量 3 室内温度 4 血氧 5 室外温度 6 海拔 7 湿度 8 空气质量
             */

            List<HealthDataHis> list = new ArrayList<>();
            HealthDataHis healthDataHis = new HealthDataHis();
            healthDataHis.setImei(imei);
            healthDataHis.setDataValue(temperate.toString());
            healthDataHis.setDataType(3);
            healthDataHis.setCreateTime(System.currentTimeMillis());   //new Timestamp(System.currentTimeMillis())
            healthDataHis.setCreatedBy("emq");
            list.add(healthDataHis);

            HealthDataHis healthDataHisTemp = new HealthDataHis();
            healthDataHisTemp.setImei(imei);
            healthDataHisTemp.setDataValue(temperature.toString());
            healthDataHisTemp.setDataType(5);
            healthDataHisTemp.setCreateTime(System.currentTimeMillis());   //new Timestamp(System.currentTimeMillis())
            healthDataHisTemp.setCreatedBy("emq");
            list.add(healthDataHisTemp);

            HealthDataHis healthDataHisElev = new HealthDataHis();
            healthDataHisElev.setImei(imei);
            healthDataHisElev.setDataValue(elevation);
            healthDataHisElev.setDataType(6);
            healthDataHisElev.setCreateTime(System.currentTimeMillis());   //new Timestamp(System.currentTimeMillis())
            healthDataHisElev.setCreatedBy("emq");
            list.add(healthDataHisElev);

            HealthDataHis healthDataHisHum = new HealthDataHis();
            healthDataHisHum.setImei(imei);
            healthDataHisHum.setDataValue(humidity);
            healthDataHisHum.setDataType(7);
            healthDataHisHum.setCreateTime(System.currentTimeMillis());   //new Timestamp(System.currentTimeMillis())
            healthDataHisHum.setCreatedBy("emq");
            list.add(healthDataHisHum);

            HealthDataHis healthDataHisAqi = new HealthDataHis();
            healthDataHisAqi.setImei(imei);
            healthDataHisAqi.setDataValue(aqi);
            healthDataHisAqi.setDataType(8);
            healthDataHisAqi.setCreateTime(System.currentTimeMillis());   //new Timestamp(System.currentTimeMillis())
            healthDataHisAqi.setCreatedBy("emq");
            list.add(healthDataHisAqi);
            simpleMongoService.insertMulti(list, MongoDbConstant.HEALTH_DATA);

            Device device = new Device();
            device.setImei(imei);
            device.setTemperate(temperate); //体温
            device.setTemperature(temperature); //环境温度
            device.setElevation(Integer.valueOf(elevation));
            device.setHumidity(humidity);
            device.setAqi(aqi);
            deviceService.updateByImeiSelective(device);

        } catch (Exception e) {
            return "4032 佩戴上报体温，环境温度，海拔成功！";
        }
        return "4032 佩戴上报体温，环境温度，海拔失败！";
    }
}
